package webBeans;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;







import beansInterfaces.BasicProductMgr;




import beansInterfaces.ConstraintsMgr;
import beansInterfaces.PackageMgr;
import dto.ExcursionDTO;
import dto.ResidenceDTO;



@ManagedBean(name="excursionSearch")
@ViewScoped
public class ExcursionSearchBean implements Serializable{


	private static final long serialVersionUID = 1L;

		@EJB
		private BasicProductMgr basicProductMgr;
		@EJB
		private PackageMgr packageMgr;
		@EJB
		private ConstraintsMgr c;
		
		@ManagedProperty(value = "#{packageCreation}")
		private PackageCreationBean pBean;
		
		@ManagedProperty(value = "#{bookingBean}")
		private BookingBean uBean;
		
		
		private List<ExcursionDTO> excursions;
		private List<ExcursionDTO> excUser;
		private ExcursionDTO currentExcursion;
		private List<ExcursionDTO> filtered;
		private List<ExcursionDTO> filteredUser;
		private ExcursionDTO modifiedExcursion;
		
		public ExcursionSearchBean() {}

		

		@PostConstruct
		public void init()
		{			 
			this.filteredUser= this.basicProductMgr.getUserExcursionsDTO();
		 	this.filtered = this.basicProductMgr.getAllExcursionsDTO();
			
			this.excursions=this.basicProductMgr.getAllExcursionsDTO();
			this.excUser=this.basicProductMgr.getUserExcursionsDTO();
			
			this.modifiedExcursion= new ExcursionDTO();
			

		}

		// Getters and Setters
		public ExcursionDTO getCurrentExcursion() {
			return currentExcursion;
		}

		public void setCurrentExcursion(ExcursionDTO currentExcursion) {
			this.currentExcursion = currentExcursion;
		}
		
		public List<ExcursionDTO> getExcursions() {
			return excursions;
		}
		
		public void setExcursions(List<ExcursionDTO> excursions) {
			this.excursions = excursions;
		}
		
		public List<ExcursionDTO> getFiltered() {
			return filtered;
		}
		
		public void setFiltered(List<ExcursionDTO> filtered) {
			this.filtered = filtered;
		}

		public void setpBean(PackageCreationBean pBean) {
			this.pBean = pBean;
		}

		public void setuBean(BookingBean uBean) {
			this.uBean = uBean;
		}
		
		public List<ExcursionDTO> getExcUser() {
			return excUser;
		}


		public void setExcUser(List<ExcursionDTO> excUser) {
			this.excUser = excUser;
		}
		
		public ConstraintsMgr getC() {
			return c;
		}

		public void setC(ConstraintsMgr c) {
			this.c = c;
		}
		
		public List<ExcursionDTO> getFilteredUser() {
			return filteredUser;
		}

		public void setFilteredUser(List<ExcursionDTO> filteredUser) {
			this.filteredUser = filteredUser;
		}
		
		public ExcursionDTO getModifiedExcursion() {
			return modifiedExcursion;
		}



		public void setModifiedExcursion(ExcursionDTO modifiedExcursion) {
			this.modifiedExcursion = modifiedExcursion;
		}
		
	

		//Employee Methods 
		




		public String modifyResidence() 
		{	
			if(!this.packageMgr.isInPack(this.modifiedExcursion)){
				
			this.basicProductMgr.updateExcursion(modifiedExcursion);
			return "/employee/indexBasicProduct?faces-redirect=true";
			}
			return "error";
		}



		public String modifyExcursion() 
		{	
			if(!this.packageMgr.isInPack(this.modifiedExcursion)){
				
			this.basicProductMgr.updateExcursion(modifiedExcursion);
			return "/employee/indexBasicProduct?faces-redirect=true";
			}
			return "error";
		}
		
		public String deleteExcursion() 
		{	
			if(!this.packageMgr.isInPack(currentExcursion) )
			{
				this.basicProductMgr.deleteExcursion(currentExcursion);
				this.pBean.resetCurrentPackage();
				return "indexBasicProduct";
			}
			return "error";
			
		}
		
		public String addToPack()
		{
			if(this.currentExcursion.getDisponibility()>0 && this.currentExcursion.isAvailable())
			{
				this.pBean.getCurrentPackage().setExcursion(this.currentExcursion);
				this.pBean.getCurrentPackage().setPrice(this.c.autoSetPrice(this.pBean.getCurrentPackage()));
				this.currentExcursion= new ExcursionDTO();
				return "createPackage";
			}
			else
				return "error";
			
		}
		
	public void passToModified(){
			
			ExcursionDTO dto = this.currentExcursion;
			this.modifiedExcursion.setId(dto.getId());
			this.modifiedExcursion.setDisponibility(dto.getDisponibility());
			this.modifiedExcursion.setAdmin(this.uBean.getUserMgr().getUserDTO().getUsername());
			this.modifiedExcursion.setPrice(dto.getPrice());
			this.modifiedExcursion.setDate(dto.getDate());
			this.modifiedExcursion.setType(dto.getType());
			this.modifiedExcursion.setDescription(dto.getDescription());
			this.modifiedExcursion.setAvailable(dto.isAvailable());
			this.modifiedExcursion.setStartTime(dto.getStartTime());
			this.modifiedExcursion.setEndTime(dto.getEndTime());
			


	}
		
		//User Methods
		public void changeExcursion(){
			if(this.currentExcursion.getDisponibility()>0 && this.currentExcursion.isAvailable())
			{
				this.uBean.getPackageOnEdit().setExcursion(this.currentExcursion);
				this.uBean.getPackageOnEdit().setPrice(this.c.autoSetPrice(this.uBean.getPackageSelected()));
				this.currentExcursion= new ExcursionDTO();
			}
			
		}
		
		


		

}
